package com.feinno.urcs.group.interop.task;

import com.feinno.rpc.channel.RpcFuture;
import com.feinno.rpc.client.RpcMethodStub;
import com.feinno.urcs.group.api.FXLogicPoolService;
import com.feinno.urcs.group.api.FXUserIndexService;
import com.feinno.urcs.group.interop.entity.*;
import com.feinno.urcs.group.siptask.task.GroupTaskEvents;
import org.helium.framework.annotations.FieldSetter;
import org.helium.framework.annotations.ServiceSetter;
import org.helium.framework.annotations.TaskImplementation;
import org.helium.framework.rpc.LegacyRpcClient;
import org.helium.framework.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 需要通过 entity 中 OperateType {@link OperateType} 判定具体业务
 *
 * @author Li.Hongbo <lihongbo@feinno.com>
 */

@TaskImplementation(event = GroupTaskEvents.GROUP_FETION_BYE)
public class FXGroupByeTask implements Task<FetionByeGroupArgs> {
	private static final Logger LOGGER = LoggerFactory.getLogger(FXGroupByeTask.class);
	private String rpcMethodNameDismiss = "DeleteGroup";
	private String rpcMethodNameQuit = "QuiteGroup";

	@FieldSetter("${FX_GROUP_SERVICE_XML}")
	private LegacyRpcClient rpcClient;
	@ServiceSetter
	private FXLogicPoolService groupLogicPool;
	@ServiceSetter
	private FXUserIndexService fxUserIndexService;

	@Override
	public void processTask(FetionByeGroupArgs args) {
		try {

//			int groupPoolId = groupLogicPool.getLogicPool(args.getGroup().getGroupId());
			FXCatalogIndex fxOperaIndex = fxUserIndexService.getFXCatalogIndex(args.getGroup().getOperatorMobileNo());
			args.getGroup().setOperatorUid(fxOperaIndex.getUserId());
			args.getGroup().setOperatorLogicPool(fxOperaIndex.getLogicalPoolId());
//			args.getGroup().setGroupLogicPool(groupPoolId);


			if(args.getMemberMobiles() !=null) {
				List<FetionUserArgs> members = args.getMemberMobiles().stream().map(member -> {
					try {
						FXCatalogIndex fxUserIndex = null;
						fxUserIndex = fxUserIndexService.getFXCatalogIndex(member.getMobileNo());
						return new FetionUserArgs(fxUserIndex.getUserId(), fxUserIndex.getLogicalPoolId());
					} catch (Exception e) {
						LOGGER.warn(String.format(" Fx dismissGroup Error GroupId=%s Mobile=%s", args.getGroup().getGroupId(), args.getGroup().getOperatorMobileNo()), e);
					}
					return null;

				}).collect(Collectors.toList());
				args.setMembers(members);
			}

			String rpcMethodName = null;
			if (args.getGroup().getOperateType() == (OperateType.Dismiss.intValue())) {
				rpcMethodName = rpcMethodNameDismiss;
			} else if (args.getGroup().getOperateType() == (OperateType.QuitGroup.intValue())) {
				rpcMethodName = rpcMethodNameQuit;
			} else {
				LOGGER.warn("OperateType No Support entity =" + args.toString());
				return;
			}
			RpcMethodStub stub = rpcClient.getMethodStub(rpcMethodName);
			RpcFuture future = stub.invoke(args);
			final String finalRpcMethodName = rpcMethodName;
			future.addListener(result ->
							LOGGER.info(String.format("invoke Fetion RPC Method:%s,retCode=%s,Args=%s", finalRpcMethodName, result.getValue().getReturnCode(), args.toString()))
			);
		} catch (Exception e) {
			LOGGER.error(String.format("process  error,Args=%s", args.toString()), e);
		}
	}
}
